package learnJava1.JDBC.domain;

import com.mysql.jdbc.ConnectionFeatureNotAvailableException;

import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class JDBCUnits {
    private static String url;
    private static String user;
    private static String password;
    private static String driver;

    static{
        Properties prop = new Properties();
        try {
            prop.load(new FileReader("D:\\IdeaProjects\\Project\\learnJava1\\jdbc.properties"));
            url = prop.getProperty("url");
            user = prop.getProperty("user");
            password = prop.getProperty("password");
            driver = prop.getProperty("driver");
            Class.forName(driver);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnectino() throws SQLException{
        return DriverManager.getConnection(url,user,password);
    }

    public static void close(Connection c, Statement stmt, ResultSet rs){
        if(c != null){
            try {
                c.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Connection c, Statement stmt){
        if(c != null){
            try {
                c.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}
